<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/3/19
 * Time: 18:10
 */

namespace bootstrap;

use support\Db;
use Workerman\Worker;
use Webman\Bootstrap;

class DBListen implements Bootstrap
{
    /**
     * @param Worker|null $worker
     *
     * @return void
     */
    public static function start(?Worker $worker)
    {
        Db::listen(function ($query){

            $sql = str_replace('%', "@@", $query->sql);
            $sql = str_replace('?', "'%s'", $sql);
            $sql = vsprintf($sql, $query->bindings);
            $sql = str_replace('@@', "%", $sql);
            $sql = "sql: ".$sql." ; [time: ".($query->time*0.001).'s]';
            if(is_object(request())){
                request()->sqllogDebug .= ($sql."\n");
            }
        });
    }
}